package tasoloikka;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Rectangle;

public class Bullet {

  private double positionX;
  private double positionY;
  private int destinationX;
  private int destinationY;
  private double lastPositionX;
  private double lastPositionY;
  private int width;
  private int height;
  private double deltaX;
  private double deltaY;
  private double mag;
  private int speed = 10;

  public Bullet(int startingPositionX, int startingPositionY, int destinationX, int destinationY, int w, int h) {
    this.positionX = startingPositionX;
    this.positionY = startingPositionY;
    this.destinationX = destinationX;
    this.destinationY = destinationY;
    this.width = w;
    this.height = h;
    

    deltaX = destinationX - positionX;
    deltaY = destinationY - positionY;
    calculatePosition();
  }

  public void paint(Graphics gbi) {
    gbi.setColor(Color.yellow);
    gbi.drawRect((int) positionX, (int) positionY, width, height);
  }

  public void moveBullet() {
    lastPositionX = positionX;
    lastPositionY = positionY;
    
    positionX += deltaX;
    positionY += deltaY;
  }

  public void calculatePosition() {
    mag = Math.sqrt(deltaX * deltaX + deltaY * deltaY);

    deltaX = deltaX / mag * speed;
    deltaY = deltaY / mag * speed;
  }

  public Rectangle getRectangle() {
    return (new Rectangle((int) positionX, (int) positionY, width, height));
  }

  //SETTERIT
  public void setPositionX(int x) {
    this.positionX = x;
  }

  public void setPositionY(int y) {
    this.positionY = y;

  }
}
